package com.lagou.server;

import com.lagou.client.ManageStudent;
import com.lagou.mmclass.Student;
import com.lagou.mmclass.User;
import com.lagou.mmclass.UserMessage;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

public class ServerView {
    /**
     *
     * 合成复用原则
     */
    private ServerInit sic;
    private ServerDao sd;
    private List<User> userList;

    public ServerView(ServerInit sic, ServerDao sd, List<User> userList) {
        this.sic = sic;
        this.sd = sd;
        this.userList = userList;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public void serverReceive() throws IOException, ClassNotFoundException {
        while (true){

//            UserMessage tum = (UserMessage) sic.getOis().readObject();
//            System.out.println("接收到的消息是"+tum);
            System.out.println("服务器等待数据的到来...");
            // 使用对象输入流来接收
            //UserMessage tum = (UserMessage) sic.getOis().readObject();
            Object obj = sic.getOis().readObject();
            System.out.println("服务器接收到的对象是：" + obj);
            if (obj instanceof UserMessage) {
                UserMessage tum = (UserMessage) obj;
                // 使用分支结构针对客户端发来的不同类型进行不同的处理
                switch (tum.getType()) {
                    case "managerCheck":
                        ServerManagerCheck(tum);
                        break;
                    case "clientExit":
                        ServerExit();
                        return;
                    case "clientAddUser":
                        ServerAddUser(tum);
                        break;
                    case "clientDeleteUser":
                        ServerDeleteUser(tum);
                    case "clientModifyUser":
                        ServerModifyUser(tum);
                    case "clientFindUser":
                        ServerFindUser(tum);
                }
            }
//            if (obj instanceof QuestionMessage) {
//                QuestionMessage tqm = (QuestionMessage) obj;
//            }


        }
    }




    /**
     *
     *管理员校验
     * @param tum
     * @throws IOException
     */
   public void ServerManagerCheck(UserMessage tum) throws IOException {

       if(sd.serverManagerCheck(tum.getUser())){
           tum.setType("success");
       }else {
           tum.setType("falil");
       }
       sic.getOos().writeObject(tum);
       System.out.println("服务器发送数据成功");

   }
    /**
     *
     *自定义成员方法实现服务器增加学员账户
     */
    public void ServerAddUser(UserMessage tum) throws IOException {
        for(User tu:userList){
            //判断集合中是否存在该学员，存在则添加失败
          if(tu.getName().equals(tum.getUser().getName())){
              tum.setType("fail");
              sic.getOos().writeObject(tum);
              System.out.println("服务器发送添加学员失败！");
          }
        }
        userList.add(tum.getUser());
        tum.setType("success");
        sic.getOos().writeObject(tum);
        System.out.println("服务器发送添加学员成功");
    }

    public void ServerDeleteUser(UserMessage tum) throws IOException {
        for (User tu:userList){
            if(tu.getName().equals(tum.getUser().getName())){
                userList.remove(tum.getUser());
                tum.setType("success");
                sic.getOos().writeObject(tum);
                System.out.println("服务端发送学员成功");
            }
        }
    }
    public void ServerModifyUser(UserMessage tum) throws IOException {
        for (User tu:userList){
            if(tu.getName().equals(tum.getUser().getName())){
                userList.remove(tu);
                userList.add(tum.getUser());
                tum.setType("success");
                sic.getOos().writeObject(tum);
                System.out.println("服务端发送修改学员成功");
            }else {
                tum.setType("fail");
                System.out.println("服务端发送修改学员失败");
            }
        }
    }
    public void ServerFindUser(UserMessage tum) throws IOException {
        for (User tu:userList){
            if(tu.getName().equals(tum.getUser().getName())){
                tum.setType("success");
                sic.getOos().writeObject(tum);
                System.out.println("服务器发送查找学员成功");
            }
            else {
                tum.setType("fail");
                System.out.println("服务器发送查找学员失败");
            }
        }
    }
    /**
     *
     * 自定义成员方法实现退出功能
     * @throws IOException
     */
    public void ServerExit() throws IOException {
        sic.serverClose();
    }


}
